package com.lx.spring.config.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;


@Aspect
public class LogAspects {

    /**
     * 抽取公共的切入点表达式
     * 1.本类引用    @Before("pointCut()")
     * 2.其它的切面引用  @After("com.lx.spring.config.aop.LogAspects.pointCut()")
     *
     */
    @Pointcut(value = "execution(public int com.lx.spring.config.bean.MathCaculator.*(..))")
    public void pointCut(){

    }


//    @Before("execution(public int com.lx.spring.config.bean.MathCaculator.div(int, int))")// 声明制定方法
//    @Before("execution(public int com.lx.spring.config.bean.MathCaculator.*(..))") //指定类中任意方法 任意参数
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println("div start "+ joinPoint.getSignature().getName());
        System.out.println("div start, params: {} "+ Arrays.toString(args));
    }

    @After("com.lx.spring.config.aop.LogAspects.pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println("div end "+ joinPoint.getSignature().getName());

    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logAfterReturning(Object result){
        System.out.println("div logAfterReturning, result: {} "+result);
    }


    @AfterThrowing(value = "pointCut()", throwing ="exception")
    public void logAfterThrowing(JoinPoint joinPoint, Exception exception){
        System.out.println("div logAfterThrowing, exception: {}" + exception.getLocalizedMessage());
    }


    @Around(value = "pointCut()")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("div logAround" );
        Object r = null;
        try {
            Object[] params = proceedingJoinPoint.getArgs();
            System.out.println("div logAround params "+Arrays.toString(params));

            r = proceedingJoinPoint.proceed();

            System.out.println("div logAround result "+ r);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return r;
    }


}
